package cn.kgc.springcloud.controller;

import cn.hutool.json.JSONUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author 课工场
 * @date 2024/4/29
 * @description
 */
@RestController
@RequestMapping("service2")
public class Service2Controller {


    @GetMapping("m1")
    @HystrixCommand(fallbackMethod = "method1Fallback")
    public String  method1(Integer id){

        if (id<0){
            throw new RuntimeException("参数异常");
        }

        return " service2 ok";
    }

    //定义快速响应方法 method1   该方法被调用会将抛出异常方法的参数传递到该方法中
    public  String  method1Fallback(Integer id){

        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("code",500);
        result.put("msg","参数异常，请检查");
        result.put("data",id);

        String jsonStr = JSONUtil.toJsonStr(result);
        return jsonStr;
    }

}
